home *** CD-ROM | disk | FTP | other *** search
/ Precision Software Appli…tions Silver Collection 1 / Precision Software Applications Silver Collection Volume One (PSM) (1993).iso / demos / devel3.exe / PLG.C < prev    next >
C/C++ Source or Header  |  1992-05-17  |  4KB  |  157 lines

  1. /* PLG file i/o */
  2.  
  3. /* Written by Bernie Roehl, March 1992 */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <alloc.h>
  8. #include "rend386.h"
  9.  
  10. /* Copyright 1992 by Dave Stampe and Bernie Roehl.
  11.    May be freely used to write software for release into the public domain;
  12.    all commercial endeavours MUST contact Bernie Roehl and Dave Stampe
  13.    for permission to incorporate any part of this software into their
  14.    products!
  15.  */
  16.  
  17. int load_err = 0;  /* set if an error was encountered during loading */
  18.  
  19. static long xshift = 0, yshift = 0, zshift = 0;
  20. static float xrescale = 1, yrescale = 1, zrescale = 1;
  21. static int depthsort = 0;
  22.  
  23. static unsigned *cmap;
  24. static int mapsize = 0;
  25.  
  26. static unsigned mapcolor(unsigned orig)
  27.     {
  28.     if ((orig & 0x8000) == 0) return orig;  /* only map if top bit set */
  29.     if (cmap == NULL) return orig;
  30.     orig &= 0x7FFF;  /* turn the top bit off */
  31.     if (orig > mapsize) return orig;
  32.     return cmap[orig];
  33.     }
  34.  
  35. void set_loadplg_colormap(unsigned *map, int msize)
  36.     {
  37.     cmap = map;
  38.     mapsize = msize;
  39.     }
  40.  
  41. void set_loadplg_offset(long x, long y, long z)
  42.     {
  43.     xshift = x;  yshift = y;  zshift = z;
  44.     }
  45.     
  46. void set_loadplg_scale(float x, float y, float z)
  47.     {
  48.     xrescale = x;  yrescale = y;  zrescale = z;
  49.     }
  50.     
  51. void set_loadplg_depthsort(int type)
  52.     {
  53.     depthsort = type;
  54.     }
  55.  
  56. void strip_comment(char *buff)  /* also trim newline character(s) */
  57.     {
  58.     char *p;
  59.     if ((p = strchr(buff, '\n')) != NULL) *p = '\0';  /* trim newline */
  60.     if ((p = strchr(buff, '#')) != NULL) *p = '\0';   /* trim comment */
  61.     if ((p = strchr(buff, '*')) != NULL) *p = '\0';   /* future expansion */
  62.     }
  63.  
  64. OBJECT *load_plg(FILE *in)
  65.     {
  66.     OBJECT *obj;
  67.     char tbuff[1000];
  68.     char objname[100], buf1[100], buf2[100];
  69.     int nv, np, i;
  70.                  /* skip blank lines */
  71.     do {
  72.         if (fgets(tbuff, sizeof(tbuff), in) == NULL) return NULL;
  73.         strip_comment(tbuff);
  74.         }
  75.     while (tbuff[0] == '\0');
  76.  
  77.     if (sscanf(tbuff, "%s %s %s", objname, buf1, buf2) != 3)
  78.         {  load_err = -1; return NULL;  }
  79.     if ((obj = new_obj(0, nv = atoi(buf1), np = atoi(buf2), objname)) == NULL)
  80.         {  load_err = -2; return NULL;  }
  81.  
  82.     for (i = 0; i < nv; ++i)       /* load in vertices */
  83.         {
  84.         float x, y, z;
  85.         /* skip blank lines */
  86.         do  {
  87.             if (fgets(tbuff, sizeof(tbuff), in) == NULL)
  88.                 { load_err = -4; return NULL; }
  89.             strip_comment(tbuff);
  90.             }
  91.         while (tbuff[0] == '\0');
  92.  
  93.         if (sscanf(tbuff, "%f %f %f", &x, &y, &z) != 3)
  94.             { load_err = -5;  return NULL;  }
  95.         add_vertex(obj, ((long) (x*xrescale))+xshift, ((long) (y*yrescale))+yshift,
  96.             ((long) (z*zrescale))+zshift);
  97.         }
  98.  
  99.     for (i = 0; i < np; ++i)   /* load polygons */
  100.         {
  101.         int j, npoints;
  102.         unsigned color;
  103.         POLY *poly;
  104.         char *p;
  105.             /* skip blank lines */
  106.         do {
  107.             if (fgets(tbuff, sizeof(tbuff), in) == NULL)
  108.                 { load_err = -6; return NULL; }
  109.             strip_comment(tbuff);
  110.             } while (tbuff[0] == '\0');
  111.  
  112.         color = (unsigned) strtoul(tbuff,&p,0) ; /* req so hex colors usable */
  113.         color = mapcolor(color);
  114.         if ((p = strtok(p, " \t")) == NULL)
  115.             { load_err = -8; return NULL; }
  116.         npoints = atoi(p);
  117.         if ((poly = add_poly(obj, color, npoints)) == NULL)
  118.             { load_err = -9;  return NULL;  }
  119.         for (j = 0; j < npoints; ++j)
  120.             {
  121.             if ((p = strtok(NULL, " \t")) == NULL)
  122.                 { load_err = -9; return NULL; }
  123.             add_point(obj, poly, atoi(p));
  124.             }
  125.         }
  126.     compute_obj(obj);
  127.     set_object_sorting(obj, depthsort);
  128.     load_err = 0;
  129.     return obj;
  130.     }
  131.  
  132.  
  133. save_plg(OBJECT *obj, FILE *out)
  134.     {
  135.     int nv, np, i;
  136.     char buff[100];
  137.  
  138.     get_obj_info(obj, &nv, &np, buff, sizeof(buff)-1);
  139.     fprintf(out, "%s %d %d\n", buff, nv, np);
  140.     for (i = 0; i < nv; ++i)
  141.         {
  142.         long x, y, z;
  143.         get_vertex_world_info(obj, i, &x, &y, &z);
  144.         fprintf(out, "%ld %ld %ld\n", x, y, z);
  145.         }
  146.     for (i = 0; i < np; ++i)
  147.         {
  148.         int j, n, verts[1000];
  149.         unsigned c;
  150.         get_poly_info(obj, i, &c, &n, verts, sizeof(verts)/sizeof(int));
  151.         fprintf(out, "0x04.4X %d", c & 0x7FFF, n);
  152.         for (j = 0; j < n; ++j) fprintf(out, " %d", verts[j]);
  153.         fprintf(out, "\n");
  154.         }
  155.     return 0;
  156.     }
  157.